<?php
/* Author: tbloc@cantho.gov.vn */
/* Created Date : 17-07-2014 10:10:51 */
class M_Khach_hang{  
//Properties
private $KH_ID;
private $KH_HOTEN;
private $KH_DIACHI;
private $KH_EMAIL;
private $KH_SDT;
private $KH_GIOITINH;
private $KH_NGAYSINH;
private $KH_AVARTAR;
private $TK_ID;
//properties set get 
public function setKH_ID($KH_ID) 
{
	$this->KH_ID = $KH_ID;
}
public function getKH_ID()
{
	return $this->KH_ID;
}	        
public function setKH_HOTEN($KH_HOTEN) 
{
	$this->KH_HOTEN = $KH_HOTEN;
}
public function getKH_HOTEN()
{
	return $this->KH_HOTEN;
}	        
public function setKH_DIACHI($KH_DIACHI) 
{
	$this->KH_DIACHI = $KH_DIACHI;
}
public function getKH_DIACHI()
{
	return $this->KH_DIACHI;
}	        
public function setKH_EMAIL($KH_EMAIL) 
{
	$this->KH_EMAIL = $KH_EMAIL;
}
public function getKH_EMAIL()
{
	return $this->KH_EMAIL;
}	        
public function setKH_SDT($KH_SDT) 
{
	$this->KH_SDT = $KH_SDT;
}
public function getKH_SDT()
{
	return $this->KH_SDT;
}	        
public function setKH_GIOITINH($KH_GIOITINH) 
{
	$this->KH_GIOITINH = $KH_GIOITINH;
}
public function getKH_GIOITINH()
{
	return $this->KH_GIOITINH;
}	        
public function setKH_NGAYSINH($KH_NGAYSINH) 
{
	$this->KH_NGAYSINH = $KH_NGAYSINH;
}
public function getKH_NGAYSINH()
{
	return $this->KH_NGAYSINH;
}	        
public function setKH_AVARTAR($KH_AVARTAR) 
{
	$this->KH_AVARTAR = $KH_AVARTAR;
}
public function getKH_AVARTAR()
{
	return $this->KH_AVARTAR;
}	        
public function setTK_ID($TK_ID) 
{
	$this->TK_ID = $TK_ID;
}
public function getTK_ID()
{
	return $this->TK_ID;
}	        
//crud methods
public function insert($db = null) {
	if ($db != null){
	$sql = "INSERT INTO `khach_hang`(                        
			`KH_ID`, 
				`KH_HOTEN`, 
				`KH_DIACHI`, 
				`KH_EMAIL`, 
				`KH_SDT`, 
				`KH_GIOITINH`, 
				`KH_NGAYSINH`, 
				`KH_AVARTAR`, 
				`TK_ID` 
		)
	VALUES(
    		:KH_ID, 
	    		:KH_HOTEN, 
	    		:KH_DIACHI, 
	    		:KH_EMAIL, 
	    		:KH_SDT, 
	    		:KH_GIOITINH, 
	    		:KH_NGAYSINH, 
	    		:KH_AVARTAR, 
	    		:TK_ID 
	    );";
	$parameters = array(
			":KH_ID", 
				":KH_HOTEN", 
				":KH_DIACHI", 
				":KH_EMAIL", 
				":KH_SDT", 
				":KH_GIOITINH", 
				":KH_NGAYSINH", 
				":KH_AVARTAR", 
				":TK_ID" 
		);
    $values = array(  
            	$this->getKH_ID(), 
	            	$this->getKH_HOTEN(), 
	            	$this->getKH_DIACHI(), 
	            	$this->getKH_EMAIL(), 
	            	$this->getKH_SDT(), 
	            	$this->getKH_GIOITINH(), 
	            	$this->getKH_NGAYSINH(), 
	            	$this->getKH_AVARTAR(), 
	            	$this->getTK_ID()
	    );
    $types = array(
   
                                    PDO::PARAM_INT,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                        PDO::PARAM_INT
                       );
	$lastInsertID = $db->executeNonQuery($sql, $parameters, $values, $types, true);
	$this->setKH_ID($lastInsertID);
    }
}
	
	public function delete($db = null) {
	    if ($db != null) {
	        $sql = "update `khach_hang`
	                set `is_active`=0
	                where `KH_ID` = :KH_ID;";
	        $parameters = array(":KH_ID");
	        $values = array($this->getKH_ID());
	        $types = array(PDO::PARAM_INT);//Kiem tra lai kieu du lieu nhe
	        $result = $db->executeNonQuery($sql, $parameters, $values, $types);
	        return $result;
	    }
	}
	
	public function update($db = null) {
	        if ($db != null) {
	            $sql = "UPDATE `khach_hang` SET 
			`KH_ID`=:KH_ID,
				`KH_HOTEN`=:KH_HOTEN,
				`KH_DIACHI`=:KH_DIACHI,
				`KH_EMAIL`=:KH_EMAIL,
				`KH_SDT`=:KH_SDT,
				`KH_GIOITINH`=:KH_GIOITINH,
				`KH_NGAYSINH`=:KH_NGAYSINH,
				`KH_AVARTAR`=:KH_AVARTAR,
				`TK_ID`=:TK_ID
			WHERE `KH_ID` = :KH_ID;";
	            $parameters = array(
	        	":KH_ID",
		        	":KH_HOTEN",
		        	":KH_DIACHI",
		        	":KH_EMAIL",
		        	":KH_SDT",
		        	":KH_GIOITINH",
		        	":KH_NGAYSINH",
		        	":KH_AVARTAR",
		        	":TK_ID"
		        );	
	$values = array(  
	        	$this->getKH_ID(),
        	        	$this->getKH_HOTEN(),
        	        	$this->getKH_DIACHI(),
        	        	$this->getKH_EMAIL(),
        	        	$this->getKH_SDT(),
        	        	$this->getKH_GIOITINH(),
        	        	$this->getKH_NGAYSINH(),
        	        	$this->getKH_AVARTAR(),
        	                $this->getTK_ID()      
        	);
    $types = array(
                                    PDO::PARAM_INT,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_INT,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_STR,
                                                       PDO::PARAM_INT
                       );
	$result = $db->executeNonQuery($sql, $parameters, $values, $types);
	return $result;
	        }
	 }
	 
	public function getByID($db = null) {
	if ($db != null) {
	$sql = "SELECT
			`KH_ID`,
				`KH_HOTEN`,
				`KH_DIACHI`,
				`KH_EMAIL`,
				`KH_SDT`,
				`KH_GIOITINH`,
				`KH_NGAYSINH`,
				`KH_AVARTAR`,
				`TK_ID`
			FROM `khach_hang` WHERE `KH_ID` = :KH_ID and `is_active`=1;";
	$parameters = array(":KH_ID");
	$values = array($this->getKH_ID());
	$types = array(PDO::PARAM_INT);//kiem tra lai kieu du lieu nhe
	$rows = $db->executeQuery($sql, $parameters, $values, $types);
	if ($rows != null) {
	$row = $rows->fetch();
		$this->setKH_ID($row->KH_ID);
		$this->setKH_HOTEN($row->KH_HOTEN);
		$this->setKH_DIACHI($row->KH_DIACHI);
		$this->setKH_EMAIL($row->KH_EMAIL);
		$this->setKH_SDT($row->KH_SDT);
		$this->setKH_GIOITINH($row->KH_GIOITINH);
		$this->setKH_NGAYSINH($row->KH_NGAYSINH);
		$this->setKH_AVARTAR($row->KH_AVARTAR);
		$this->setTK_ID($row->TK_ID);
	}
	return $this;
		}
	}
	
	public function getList($db = null) {
	if ($db != null) {
    $sql = "SELECT
			`KH_ID`,
				`KH_HOTEN`,
				`KH_DIACHI`,
				`KH_EMAIL`,
				`KH_SDT`,
				`KH_GIOITINH`,
				`KH_NGAYSINH`,
				`KH_AVARTAR`,
				`TK_ID`
			FROM `khach_hang` WHERE `is_active`=1;";
    $rows = $db->executeQuery($sql);
    $objects = array();
    if ($rows != null) {
    while ($row = $rows->fetch()) {
    $obj = new khach_hang();
    	 $obj->setKH_ID($row->KH_ID);
		 $obj->setKH_HOTEN($row->KH_HOTEN);
		 $obj->setKH_DIACHI($row->KH_DIACHI);
		 $obj->setKH_EMAIL($row->KH_EMAIL);
		 $obj->setKH_SDT($row->KH_SDT);
		 $obj->setKH_GIOITINH($row->KH_GIOITINH);
		 $obj->setKH_NGAYSINH($row->KH_NGAYSINH);
		 $obj->setKH_AVARTAR($row->KH_AVARTAR);
		 $obj->setTK_ID($row->TK_ID);
		$objects[] = $obj;
    }
    return $objects;
    }
        }
    }
}
?>

